package aps.module_Core;

//vanilla minecraft
import net.minecraft.src.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import aps.module_Core.module_Core.APSBlockTypes;
import buildcraft.api.core.Position;
import buildcraft.api.power.IPowerProvider;
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerFramework;
import buildcraft.core.IMachine;
import buildcraft.energy.PneumaticPowerProvider;
//buildcraft
//APS

public abstract class TileEntityAPSPowered extends TileEntityAPS implements IPowerReceptor, IMachine
{	
	int PoweredTex;
	protected ForgeDirection PowerOutDirection;
	protected IPowerProvider powerProvider; //Updated to IPowerProvider - Seph
	
	public TileEntityAPSPowered(int BottomTex, int TopTex, int DefSideTex, int PoweredSideTex, int latency, int minPower, int maxPower, int activationEnergy, int maxStoredPower, APSBlockTypes type)
	{
		super(BottomTex, TopTex, DefSideTex, type);
		PoweredTex = PoweredSideTex;
		powerProvider = PowerFramework.currentFramework.createPowerProvider();
		powerProvider.configure(latency, minPower, maxPower, activationEnergy, maxStoredPower);
	}
	
	@Override
	public void updateEntity()
	{
		resetAllTextures();
		powerProvider.update((IPowerReceptor) this);
		if(PowerOutDirection != null) setTexture(PowerOutDirection.ordinal(), PoweredTex);
		for(ForgeDirection O : ForgeDirection.VALID_DIRECTIONS) {if(powerProvider.isPowerSource(O)) setTexture(O.ordinal(), PoweredTex);}
	}
	
	protected boolean isPoweredDirection(int Direction)
	{
		Position pos = new Position (xCoord, yCoord, zCoord, PowerOutDirection);
		
		pos.moveForwards(1);
		
		return isPoweredTile((TileEntity) worldObj.getBlockTileEntity((int) pos.x, (int) pos.y, (int) pos.z));
	}
	
	protected boolean isPoweredTile (TileEntity tile)
	{
		if (tile instanceof IPowerReceptor)
		{
			IPowerProvider prov = ((IPowerReceptor) tile).getPowerProvider(); //Changed to IPowerProvider - Seph
			return prov != null && prov.getClass().equals(PneumaticPowerProvider.class);	
		}
		return false;
	}
	
	protected ForgeDirection getPoweredNeighbour()
	{
		ForgeDirection o = ForgeDirection.values()[1];
		if (isPoweredTile(worldObj.getBlockTileEntity((int) xCoord + 1, (int) yCoord, (int) zCoord))) return o.values()[5];
		else if (isPoweredTile(worldObj.getBlockTileEntity((int) xCoord - 1, (int) yCoord, (int) zCoord))) return o.values()[4];
		
		else if (isPoweredTile(worldObj.getBlockTileEntity((int) xCoord, (int) yCoord, (int) zCoord + 1))) return o.values()[3];
		else if (isPoweredTile(worldObj.getBlockTileEntity((int) xCoord, (int) yCoord, (int) zCoord - 1))) return o.values()[2];
		
		else return null;
	}
	
	public void doWork() {}
	
	public IPowerProvider getPowerProvider() {return powerProvider;}
	
	public void setPowerProvider(IPowerProvider Provider) {powerProvider = Provider;}
	
	public boolean isActive() {
		return false;
	}

	public boolean manageLiquids() {
		return false;
	}

	public boolean manageSolids() {
		return false;
	}

	public boolean allowActions() {
		return false;
	}
}